# -*- coding:utf-8 -*-
import pandas as pd
import numpy as np
import os
from datetime import date
# 源文件路径
S_STYLES_DIR = '../source/styles'
S_CONCEPT_DIR = '../source/concept'
S_EXPONENT_DIR = '../source/exponent'
S_TDX_1IND_DIR = '../source/tdx_1ind'
S_TDX_CSRC_IND_DIR = '../source/tdx_csrc_ind'
# 输出路径
O_STYLES_DIR = '../out/styles'
O_CONCEPT_DIR = '../out/concept'
O_EXPONENT_DIR = '../out/exponent'
O_TDX_1IND_DIR = '../out/tdx_1ind'
O_TDX_CSRC_IND_DIR = '../out/tdx_csrc_ind'
# 主文件路径
MAIN_FILE = '../source/沪深A股20210618.csv'

"""
1.主文件读取
2.在路径下读取源文件
3.循环处理源文件和主文件
4.输出处理后的文件
"""


def get_source_filepath(dir_path=None):
    """
    返回路径下文件文件路径列表
    """
    if dir_path is None:
        return False
    source_data = os.walk(dir_path)
    s_files = []
    for root, dirs, files in source_data:
        for file in files:
            filepath = os.path.join(root, file)
            s_files.append(filepath)
    return s_files


def handel_data(type_=None):
    """
    处理数据返回主文件的DataFrame
    1.将所属风格，概念，指数等拼接在 相应的列名下
    """
    if type_ is None:
        return False
    elif type_ == 'styles':
        column_ = '风格'
        file_dir = S_STYLES_DIR
    elif type_ == 'concept':
        column_ = '概念'
        file_dir = S_CONCEPT_DIR
    elif type_ == 'exponent':
        column_ = '指数'
        file_dir = S_EXPONENT_DIR
    elif type_ == 'tdx_1ind':
        column_ = '一级行业'
        file_dir = S_TDX_1IND_DIR
    elif type_ == 'tdx_csrc_ind':
        column_ = '证监会行业'
        file_dir = S_TDX_CSRC_IND_DIR

    files_ = get_source_filepath(file_dir)

    # 读取主文件数据 设置第二列“代码”列为索引
    main_data = pd.read_csv(
        MAIN_FILE, dtype={'代码': str}, encoding='gbk', index_col='代码')

    main_data[column_] = ''
    # 循环更新处理主文件信息数据
    # names = ''
    for file in files_:

        names = os.path.basename(file).split('_')[0]
        # 设置第一列代码列为索引
        s_data = pd.read_csv(
            file, dtype={'代码': str}, encoding='gbk', index_col='代码')

        for i in s_data.index:
            temp = main_data[column_].get(i)
            if temp is not None:
                main_data.loc[i, column_] = main_data.at[i, column_]+names+'_'
    # 拼接数字列信息
    main_data['num'] = '0.000'
    # 设置无数据的行显示'--'
    main_data[column_] = np.where(
        main_data[column_] == '', '--', main_data[column_])

    main_data.fillna('--')
    # 返回主文件数据信息
    return main_data


def save_file(type_=None):
    """
    保存数据
    """
    if type_ is None:
        return False
    elif type_ == 'styles':
        file_name = '沪深A股_风格'
        column_ = '风格'
        out_file_dir = O_STYLES_DIR
    elif type_ == 'concept':
        file_name = '沪深A股_概念'
        column_ = '概念'
        out_file_dir = O_CONCEPT_DIR
    elif type_ == 'exponent':
        file_name = '沪深A股_指数'
        column_ = '指数'
        out_file_dir = O_EXPONENT_DIR
    elif type_ == 'tdx_1ind':
        file_name = '沪深A股_通达信一级行业'
        column_ = '一级行业'
        out_file_dir = O_TDX_1IND_DIR
    elif type_ == 'tdx_csrc_ind':
        file_name = '沪深A股_通达信证监会行业'
        column_ = '证监会行业'
        out_file_dir = O_TDX_CSRC_IND_DIR 

    exprot_file_path = out_file_dir+'/'+file_name + \
        '_'+date.today().strftime('%Y%m%d')+'.txt'
    main_data = handel_data(type_)

    # 重置索引为index
    main_data = main_data.reset_index()
    # 代码列补零操作
    main_data['代码'] = ['%06d' % i for i in main_data['代码']]
    # print(main_data)
    # 不导出表头
    main_data.to_csv(exprot_file_path, columns=[
                     '市场标识', '代码', column_, 'num'], encoding='gbk', header=None, index=False)
    print('*'*10)
    print(column_+'数据转化完毕！')
    print('*'*10)


if __name__ == "__main__":
    # 概念
    save_file(type_='concept')
    # 风格
    save_file(type_='styles')
    # 指数
    save_file(type_='exponent')
    # 通达信一级行业
    save_file(type_='tdx_1ind')
    # 通达信证监会行业
    save_file(type_='tdx_csrc_ind')
